<?php 
namespace plugins\logistics\model;
use think\Model;
use plugins\store\Common;
use think\Validate;
/**
 * 评论模型
 */
class LogisticsModel extends Model
{
    private  $com = '';

    protected function _initialize()
    {
        $this->com = new Common;
    }
    /**
     * 添加发货订单
     */
    public function deliveryInfo( $data )
    {
        

        $validate = new Validate([
            'order_num'  => 'require|max:25',
            'name' => 'require',
            'mobile' => 'require|max:11',
            'province' => 'require',
            'city' => 'require',
            'area' => 'require',
            'address' => 'require',
            'store_name'=>'require',
            'order_time'=>'require'
        ]);
        if (!$validate->check($data)) {
            return $validate->getError();
        }
        $com = new Common;
        $allow = $com->getAllowAccess();
        if($allow == 'true'){
            $data['store_id'] = 'admin';  
        }else{
            $data['store_id'] = $allow; 
        }
        
        if(db('logistics_order')->where('order_num',$data['order_num'])->find()){
            return zy_sys_echo(false,'订单已存在','',100);
        }
        $res = db('logistics_order')->insert($data); 
        if($res) return zy_sys_echo(true ,'成功','',200);
    }

    /**
     * 拆分详细地址
     */
    
    function handleAddress($address ='广东省深圳市龙华新区大浪街道同胜科技大厦')
    { 
       preg_match('/(.*?(省|自治区|北京市|天津市))/',$address,$matches);
       if( count($matches) > 1) {
           $province = $matches[count($matches) - 2];
           $address = str_replace($province,'',$address);
       }
       preg_match('/(.*?(市|自治州|地区|区划|县))/',$address,$matches);
       if( count($matches) > 1) {
           $city = $matches[count($matches) - 2];
           $address=str_replace($city,'',$address);
       }
       preg_match('/(.*?(区|县|镇|乡|街道))/',$address,$matches);
       if( count($matches) > 1) {
           $area = $matches[count($matches) - 2];
           $address = str_replace($area,'',$address);
       }
       return[
           'ProvinceName'=>isset($province)?$province:'',
           'CityName'=>isset($city)?$city:'',
           'ExpAreaName'=>isset($area)?$area:'',
           'Address'=>$address
       ];
    }


    /**
     * 确认收货
     * @param  $order_num   订单编号
     * @return \返回json字符串
     */
    public function confirmReceipt($order_num = '')
    {
        if( empty($order_num) ) return zy_sys_echo(false,'未传订单编号','',100);

        $res = db('logistics_order')->where('order_num',$order_num)->update(['status'=>4]);

        if($res) return zy_sys_echo(true,'已签收','',200);
        return zy_sys_echo(false,'签收失败','',100);
        
    }

    /**
     * 发货时间
     */
    public function deliveryTime($order_num = '')
    {
        checkParam(['order_num'=>['type'=>'require','message'=>['require'=>'必填']]],['order_num'=>$order_num]);

        $time = db('logistics_order')->where(['order_num'=>$order_num,'status'=>3])->find()['delivery_time'];

        return $time;
    }

    /**
     * 查询物流
     * @param  $isModule
     * @param  $ShipperCode   快递公司编码
     * @param  $LogisticCode  物流单号
     * @param  $store  店铺id
     * @return \返回json字符串
     */
    public function query($ShipperCode='',$LogisticCode='',$store='')
    {
        $order_num = $this->request->post('order_num');
        if(!empty($order_num)){
            $data = db('logistics_order')->where('order_num',$order_num)->find();
            if(!$data) return zy_sys_echo(false,'系统数据错误','',1000);
            $ShipperCode = $data['code'];
            $LogisticCode = $data['logistics_code'];
            $store = $data['store_id'];            
        }

        if(empty($LogisticCode)) return zy_sys_echo(false,'暂无物流信息');

        $res = $this->API->OrderTraces($ShipperCode,$LogisticCode,$store); 

        if(!empty($res['status'])) 
        {
            if($res['status'] == 'success'){
                $res = $res['data'];  
            }
            
        }else{
            $res = [];
        }
           
        return zy_sys_echo(true,'结果',$res,200);
    }


}